﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WsdShop.IService;
using WsdShop.Model;
using WsdShop.Model.SearchModels;
using SqlSugar;
using Infrastructure.Dao;
using Infrastructure;
using WsdShop.Model.ViewModels;
using ServiceStack;

namespace WsdShop.Service
{
    public class ExchangeService : IExchangeService
    {
        public ProductExchange FindProductExchange(int id)
        {
            using (var Db = DbConfig.GetInstance())
            {
                return Db.Queryable<ProductExchange>().SingleOrDefault(t => t.Id == id);
            }
        }

        public List<ProductExchange> ExchangeManage(ExchangeSearchModel search, out int total)
        {
            using (var Db = DbConfig.GetInstance())
            {
                Sqlable sable = Db.Sqlable().From<ProductExchange>("");
                if (!search.Keywords.IsNullOrEmpty())
                {
                    sable.Where(" (Name like @Name or BarCode like @BarCode) ");
                }

                if (search.IsDisplayParm > 0)
                {
                    sable.Where(" IsDisplay=@IsDisplay ");
                }

                if (search.OrderByFields.IsNullOrEmpty())
                {
                    search.OrderByFields = "Id DESC";
                }

                var pars = new { Name = "%" + search.Keywords + "%", BarCode = "%" + search.Keywords + "%", IsDisplay = (search.IsDisplayParm == 1 ? 1 : 0) };

                total = sable.Count(pars);

                return sable.SelectToPageList<ProductExchange>("*", search.OrderByFields, search.Skip / search.Take + 1, search.Take, pars);
            }
        }



        public Result DelExchange(int Id, int currentUserId)
        {
            using (var Db = DbConfig.GetInstance())
            {
                var product = Db.Queryable<ProductExchange>().Single(m => m.Id == Id);
                if (product == null || product.Id == 0)
                {
                    return Result.Failure("该数据不存在，请刷新后重试");
                }
                product.IsDeleted = true;
                product.EditTime = DateTime.Now;
                product.EditUserId = currentUserId;
                if (Db.Update(product))
                {
                    return Result.Success();
                }
                else
                {
                    return Result.Failure("数据更新失败，请刷新后重试");
                }
            }
        }



        public Result ChangeDisplay(int Id, int currentUserId)
        {
            using (var Db = DbConfig.GetInstance())
            {
                var product = Db.Queryable<ProductExchange>().Single(m => m.Id == Id);
                if (product == null || product.Id == 0)
                {
                    return Result.Failure("该数据不存在，请刷新后重试");
                }
                product.IsDisplay = !product.IsDisplay;
                product.EditTime = DateTime.Now;
                product.EditUserId = currentUserId;
                if (Db.Update(product))
                {
                    return Result.Success();
                }
                else
                {
                    return Result.Failure("数据更新失败，请刷新后重试");
                }
            }
        }


        public ProductExchangeResponse ExchangeEdit(int Id)
        {
            using (var Db = DbConfig.GetInstance())
            {
                var response = new ProductExchangeResponse { SubImgUrls = new List<string>() };

                if (Id > 0)
                {
                    var model = Db.Queryable<ProductExchange>().SingleOrDefault(m => m.Id == Id);
                    if (model != null)
                    {
                        response = model.ConvertTo<ProductExchangeResponse>();
                        if (!model.ImgUrls.IsNullOrEmpty())
                        {
                            response.SubImgUrls = model.ImgUrls.FromJson<List<string>>();
                        }
                    }
                }
                return response;
            }
        }


        public Result ExchangeEditSubmit(ProductExchangeResponse ExchangeModel, int currentUserId)
        {
            if (ExchangeModel == null)
            {
                return Result.Failure("参数有误");
            }
            using (var Db = DbConfig.GetInstance())
            {
                var model = ExchangeModel.ConvertTo<ProductExchange>();

                model.ImgUrls = ExchangeModel.SubImgUrls.ToJson();

                model.AddUserId = (model.Id == 0 ? currentUserId : model.AddUserId);
                model.EditTime = DateTime.Now;
                model.EditUserId = (model.Id == 0 ? 0 : currentUserId);

                Db.InsertOrUpdate(model);
                return Result.Success();
            }
        }

        public List<MemberLevel> GetMemberLevels()
        {
            using (var Db = DbConfig.GetInstance())
            {
                return Db.Queryable<MemberLevel>().OrderBy(o => o.Id).ToList();
            }
        }


        #region Wap

        public List<ProductExchange> GetProductExchange(int Skip, int Take, out int total)
        {
            using (var Db = DbConfig.GetInstance())
            {
                total = Db.Queryable<ProductExchange>().Where(m => m.IsDisplay).OrderBy(" Sort Desc,Id Desc ").Count();
                return Db.Queryable<ProductExchange>().Where(m => m.IsDisplay).OrderBy(" Sort Desc,Id Desc ").Skip(Skip).Take(Take).ToList();
            }
        }

        public ProductExchangeDetailResponse ProductExchangeDetail(int Id, int currentMemberId)
        {
            using (var Db = DbConfig.GetInstance())
            {
                var response = new ProductExchangeDetailResponse { SubImgUrls = new List<string>() };

                if (Id > 0)
                {
                    var model = Db.Queryable<ProductExchange>().SingleOrDefault(m => m.Id == Id);
                    if (model != null)
                    {
                        response = model.ConvertTo<ProductExchangeDetailResponse>();
                        if (!model.ImgUrls.IsNullOrEmpty())
                        {
                            response.SubImgUrls = model.ImgUrls.FromJson<List<string>>();
                        }

                        var member = Db.Queryable<Member>().SingleOrDefault(m => m.Id == currentMemberId);
                        if (member.MemberLevel >= model.ExchangeLevel)
                        {
                            response.CanExchange = true;
                        }
                        else
                        {
                            response.CanExchange = false;
                        }

                        var memberlevel = Db.Queryable<MemberLevel>().SingleOrDefault(m => m.Id == model.ExchangeLevel);
                        if (memberlevel != null || memberlevel.Id > 0)
                        {
                            response.LevelLimitText = "仅限" + memberlevel.LevelName + "及以上兑换";
                        }
                        return response;
                    }
                }
                return null;
            }
        }

        #endregion

    }
}
